<template>
  <div class="ele-body">
    <el-card shadow="never">
      <!-- 搜索表单 -->
      <el-form
        :model="where"
        label-width="80px"
        class="ele-form-search"
        @keyup.enter.native="reload"
        @submit.native.prevent>
        <el-row :gutter="15">
          <el-col :md="5" :sm="5">
            <el-form-item label="起始日期:">
              <el-date-picker v-model="where.riqiBegin" type="date" format='yyyy-MM-dd' value-format='yyyy-MM-dd' placeholder="选择日期" clearable class="ele-fluid"></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :md="5" :sm="5">
            <el-form-item label="结束日期:">
              <el-date-picker v-model="where.riqiEnd" type="date" format='yyyy-MM-dd' value-format='yyyy-MM-dd' placeholder="选择日期" clearable class="ele-fluid"></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :lg="4" :md="4">
            <el-form-item label="状态:">
              <el-select
                clearable
                v-model="where.status"
                placeholder="请选择状态"
                class="ele-fluid">
                <el-option label="待处理" value="0"/>
                <el-option label="已导入" value="1"/>
                <el-option label="已删除" value="-1"/>
                <el-option label="已开票" value="2"/>
                <el-option label="已配送" value="3"/>
                <el-option label="已结束" value="5"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :lg="5" :md="5">
            <el-form-item label="关键字:">
              <el-input
                clearable
                v-model="where.keyword"
                placeholder=""/>
            </el-form-item>
          </el-col>
          <el-col :lg="5" :md="12">
            <div class="ele-form-actions">
              <el-button
                type="primary"
                icon="el-icon-search"
                class="ele-btn-icon"
                size="small"
                @click="reload">查询数据
              </el-button>
              <el-button v-if="false"
                type="primary"
                icon="el-icon-upload"
                class="ele-btn-icon"
                size="small"
                @click="exportToNC">填报上传
              </el-button>
              <el-button v-if="false"
                type="warning"
                icon="el-icon-"
                class="ele-btn-icon"
                size="small"
                @click="responseOrder">订单确认
              </el-button>
              <el-button @click="reset" size="small">重置</el-button>
            </div>
          </el-col>
        </el-row>
      </el-form>
      <!-- 数据表格 -->
      <ele-pro-table
        ref="table"
        :where="where"
        :datasource="url"
        :columns="columns"
        :selection.sync="selection"
        :pageSize="100"
        height="calc(100vh - 270px)">
        <!-- 表头工具栏 -->
        <template slot="toolbar">
          <el-button
            size="small"
            type="primary"
            icon="el-icon-plus"
            class="ele-btn-icon"
            @click="openEdit(null)"
            v-if="permission.includes('sys:yq005:add')">新增
          </el-button>
          <el-button
            size="small"
            type="danger"
            icon="el-icon-delete"
            class="ele-btn-icon"
            @click="removeBatch"
            v-if="permission.includes('sys:yq005:delete')">删除
          </el-button>
          <el-button
            size="small"
            type="warning"
            icon="el-icon-save"
            class="ele-btn-icon"
            @click="submitOrder()"
            v-if="permission.includes('sys:yq005:add1')">提交订单
          </el-button>
          <el-button
            size="small"
            type="warning"
            icon="el-icon-save"
            class="ele-btn-icon"
            @click="confirmOrder()"
            v-if="permission.includes('sys:yq005:add1')">确认订单
          </el-button>
        </template>
        <!-- 操作列 -->
        <!-- 操作列 -->
        <template slot="action" slot-scope="{row}">
          <el-link
            type="primary"
            :underline="false"
            icon="el-icon-upload2"
            @click="openEdit(row)"
          >NC回写
          </el-link>
          <el-link
            type="success"
            slot="reference"
            :underline="false"
            icon="el-icon-document"
            @click="openLog(row)"
          >日志
          </el-link>
          <el-link v-if="false"
                   type="primary"
                   :underline="false"
                   icon="el-icon-search"
                   @click="openDetail(row)"
          >单据明细
          </el-link>

        </template>
        <template slot="status" slot-scope="{row}">
          <el-tag
            :type="['warning', 'primary', 'success'][row.status]"
            size="mini">
            {{ ['未提交', '已提交', '已确认'][row.status] }}
          </el-tag>
        </template>
        <template slot="ddbh" slot-scope="{row}">
          <el-link
            type="primary"
            :underline="false"
            @click="openEdit(row)"
          >{{row.ddbh?row.ddbh:row.replaceno}}
          </el-link>
        </template>
          <template slot="yymc" slot-scope="{row}">
            <el-link
              type="primary"
              :underline="false"
              @click="openEdit(row)"
            >{{row.customername}}
            </el-link>
        </template>


      </ele-pro-table>
    </el-card>
    <!-- <LogList
    :dataItem="current"
    :visible.sync="showLog"
    /> -->
    <!-- <BillDetail
    :dataItem="current"
    :visible.sync="showDetail"
    /> -->

    <!-- 编辑弹窗 -->
    <OrderEdit
      :data="current"
      :visible.sync="showEdit"
      @done="reload"/>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
// import LogList from '../../purchase/inbound/loglist.vue';
//import BillDetail from './billdetail.vue';
import OrderEdit from './order-edit';

export default {
  name: 'ygorder_order',
  // components: {C37InfCkScBillNewEdit},
  components: {OrderEdit},
  computed: {
    ...mapGetters(["permission"]),
  },
  data() {
    return {
      // 表格数据接口
      url: '/yyq005/index',
      xylxName:'',
      xylxList:[
        {
          label:'全量响应',
          value:'00'
        },
        {
          label:'部分响应',
          value:'01'
        },
        {
          label:'不予响应',
          value:'02'
        },
      ],
      // 表格列配置
      columns: [
        {
          columnKey: 'selection',
          type: 'selection',
          width: 45,
          align: 'center',
          fixed: "left"
        },
        {
          prop: 'ddbh',
          label: '订单编号',
          showOverflowTooltip: true,
          minWidth: 130,
          align: 'left',
          slot : 'ddbh'
        },
        {
          prop: 'customername',
          label: '医院名称',
          showOverflowTooltip: true,
          minWidth: 260,
          align: 'left',
        },
        {
          prop: 'psdbm',
          label: '配送点编码',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'center'
        },
        {
          prop: 'shaddress',
          label: '配送点地址',
          showOverflowTooltip: true,
          minWidth: 180,
          align: 'center'
        },
        {
          prop: 'salesman',
          label: '业务员',
          showOverflowTooltip: true,
          minWidth: 80,
          align: 'center'
        },
        {
          prop: 'orderdate',
          label: '订单日期',
          showOverflowTooltip: true,
          minWidth: 140,
          align: 'center',
          formatter: (row, column, cellValue) => {
            return this.$util.toDateString(cellValue);
          }
        },
        {
          prop: 'status',
          label: '状态',
          showOverflowTooltip: true,
          minWidth: 80,
          align: 'center',
          slot: 'status'
        },
        //   {
        //   columnKey: 'action',
        //   label: '操作',
        //   width: 160,
        //   align: 'center',
        //   resizable: false,
        //   slot: 'action',
        //   fixed: "right"
        // }
      ],
      // 表格搜索条件
      where: {riqiBegin:this.getFromDate(),riqiEnd:new Date(),keyword:''},
      // 表格选中数据
      selection: [],
      // 当前编辑数据
      current: null,
      // 是否显示编辑弹窗
      showEdit: false,
      showLog: false,
      showDetail:false,
    };
  },
  methods: {
    statusFormat(row, column,cellValue) {
      const daterc = row[column.property]
      if(daterc !=null){
        return cellValue=="N"?"已同步":"未同步";
      }
    },
    getFromDate() {
      const timeOne = new Date()
      const year = timeOne.getFullYear()
      let month = timeOne.getMonth()
      let day = 1
      //month = month < 10 ? '0' + month : month
      //day = day < 10 ? '0' + day : day
      ///const NOW_MONTHS_AGO = `${year}-${month}-${day}`
      return new Date(year,month,day);
    },
    /* 刷新表格 */
    reload() {
      this.$refs.table.reload({where: this.where});
    },
    /* 重置搜索 */
    reset() {
      this.where = {riqiBegin:this.getFromDate(),riqiEnd:new Date()};
      this.reload();
    },

    openLog(row){
      console.log(row);
      this.current = row;
      this.showLog = true;
    },
    openEdit(row)
    {
      this.current = row;
      this.showEdit = true;
    },
    openDetail(row){
      this.current = row;
      this.showDetail = true;
    },
    optionClick(data){
      var item = this.xylxList.find((item)=>item.value===data)
      this.xylxName = item.label;
    },
    submitOrder(){
      let param = {
        yq005:this.form,
        yq005Details:this.invList
      };
      if(this.selection.length==0)
      {
        this.$message.info('请选择需要提交的代填订单。');
        return;
      }
      if(Number(this.this.selection[0].status)!=0)
      {
        this.$message.info('请选择未提交状态的订单。');
        return;
      }
      this.$confirm('确定要提交选中的代填订单吗?', '提示', {
        type: 'warning'
      }).then(() => {
        this.$http.post('/yyq005/dddtupload', param).then(res => {
          if (res.data.code === 0) {
            this.$message.success(res.data.msg);
            //this.replaceno = res.data.data;
          } else {
            this.$message.error(res.data.msg);
          }
          //加载日志列表
          this.loadlog();
        }).catch(e => {
          this.$message.error(e.message);
        });
      });

    },
    confirmOrder(){
      let param = {
        yq005:this.form,
        yq005Details:this.invList
      };

      this.$http.post('/yyq005/dddtconfirm', param).then(res => {
        if (res.data.code === 0) {
          this.$message.success(res.data.msg);
          //this.replaceno = res.data.data;
          //加载日志列表
          this.loadlog();
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        this.$message.error(e.message);
      });

    },
    /* 订单响应 */
    responseOrder(){
      if(!this.where.xylx)
      {
        this.$message.info('请选择需要提交的的订单响应类型。');
        return;
      }
      if(this.selection.length==0)
      {
        this.$message.info('请选择需要响应的阳光平台订单。');
        return;
      }
      this.$confirm('确定要将选中的订单进行【'+this.xylxName+'】吗?', '提示', {
        type: 'warning'
      }).then(() => {
        const loading = this.$loading({lock: true});
        this.$http.post('/ygorder/orderresponse', {ddmxbhs:this.selection.map(d => d.ddmxbh),czlx:this.where.xylx}).then(res => {
          loading.close();
          if (res.data.code === 0) {
            this.$message.success(res.data.msg);
            this.reload();
          } else {
            this.$message.error(res.data.msg);
          }
        }).catch(e => {
          loading.close();
          this.$message.error(e.message);
        });
      }).catch(() => {
      });
    },
    /* 显示编辑 */
    exportToNC() {
      if(this.selection.length==0)
      {
        this.$message.info('请选择需要提交至NC的单据。');
        return;
      }
      this.$confirm('确定要将选中的单据提交至NC吗?', '提示', {
        type: 'warning'
      }).then(() => {
        const loading = this.$loading({lock: true});
        this.$http.post('/ygorder/saleordernc63', {ddmxbhs:this.selection.map(d => d.ddmxbh),startDate:this.where.riqiBegin}).then(res => {
          loading.close();
          if (res.data.code === 0) {
            this.$message.success(res.data.msg);
            this.reload();
          } else {
            this.$message.error(res.data.msg);
          }
        }).catch(e => {
          loading.close();
          this.$message.error(e.message);
        });
      }).catch(() => {
      });
    },
    /* 删除 */
    remove(row) {
      const loading = this.$loading({lock: true});
      this.$http.delete('/c37infckscbillnew/delete', {id: row.id}).then(res => {
        loading.close();
        if (res.data.code === 0) {
          this.$message.success(res.data.msg);
          this.reload();
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        loading.close();
        this.$message.error(e.message);
      });
    },
    /* 批量删除 */
    removeBatch() {
      if (this.selection.length!=1) {
        this.$message.info('请选择一条代填订单后删除');
        return;
      }
      if(Number(this.selection[0].status)!=0){
        this.$message.info('已提交或已确认的订单不能删除。');
        return;
      }
      this.$confirm('确定要删除选中的代填订单吗?', '提示', {
        type: 'warning'
      }).then(() => {
        const loading = this.$loading({lock: true});
        this.$http.delete('/yyq005/delete/'+this.selection.map(d => d.replaceno)).then(res => {
          loading.close();
          if (res.data.code === 0) {
            this.$message.success(res.data.msg);
            this.reload();
          } else {
            this.$message.error(res.data.msg);
          }
        }).catch(e => {
          loading.close();
          this.$message.error(e.message);
        });
      }).catch(() => {
      });
    },
  }
}
</script>

<style scoped>
</style>
